/*
 * RouteLink.java
 *
 * Created on February 25, 2007, 9:05 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.core.routenet;

import org.fpdev.core.transit.*;
import java.awt.Color;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;

/**
 *
 * @author demory
 */
public class RLinkSub extends RLink {

  private SubRoute sub_;
  private RNode fNode_,  tNode_;
  private int startDLI_,  endDLI_;

  /** Creates a new instance of CRouteLink */
  public RLinkSub(SubRoute sub, RNode fNode, RNode tNode, int startDLI, int endDLI) {
    sub_ = sub;
    startDLI_ = startDLI;
    endDLI_ = endDLI;
    fNode_ = fNode;
    tNode_ = tNode;
  }

  public String getID() {
    return sub_.getFullID() + "/" + startDLI_ + "-" + endDLI_;
  }

  public RNode getFromNode() {
    return fNode_;
  }

  public RNode getToNode() {
    return tNode_;
  }

  public int getStartDLI() {
    return startDLI_;
  }

  public int getEndDLI() {
    return endDLI_;
  }

  public int getType() {
    return RLINK_SUBROUTE;
  }

  public SubRoute getSubRoute() {
    return sub_;
  }

  public int timeCostFW(int inTime, CalendarService service, double walkSpeedFPS, int transferTime) {
    if (!sub_.hasTable(service)) {
      return 100000000;
    }
    //if(sub_.getRoute().getID().equals("9")) System.out.println("tcFW/sub "+getID()+" @ "+CUtil.sTimeToStr(inTime));
    int run = sub_.getNextRun(service, inTime + transferTime, startDLI_);
    if (run == 0) {
      return 100000000;
    }
    int outTime = sub_.getTime(service, run, endDLI_ + 1);
    lastRun_ = run;
    //if(sub_.getRoute().getID().equals("9")) System.out.println(" * lastRun_="+lastRun_);
    return (outTime - inTime);
  }

  public int timeCostBW(int outTime, CalendarService service, double walkSpeedFPS, int transferTime) {
    if (!sub_.hasTable(service)) {
      return 100000000;
    }
    int run = sub_.getPrevRun(service, outTime - transferTime, endDLI_);
    if (run == 0) {
      return 100000000;
    }
    int inTime = sub_.getTime(service, run, startDLI_);
    lastRun_ = run;
    return (outTime - inTime);
  }

  public void draw(MapCanvas canvas) {
    canvas.setColor(new Color(127, 127, 255));
    canvas.drawLine(getFromNode().getX(), getFromNode().getY(), getToNode().getX(), getToNode().getY(), true);
  }
}
